package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.AntiepidemicPurcMapper;
import com.suyuan.dao.FeedsMapper;
import com.suyuan.dao.FeedsinMapper;
import com.suyuan.dao.VaccineInforMapper;
import com.suyuan.entity.*;
import com.suyuan.service.FeedsInStoreService;
import com.suyuan.service.VaccInStoreService;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service(value = "feedsInStoreService")
public class FeedsInStoreServiceImpl implements FeedsInStoreService {

    @Resource
    private FeedsinMapper feedsinMapper;
    @Resource
    private FeedsMapper feedsMapper;

    public PageInfo inquiry(Integer feedId, Date depositTime, String remark, Page page) {
        FeedsinExample feedsinExample=new FeedsinExample();
        FeedsinExample.Criteria criteria=feedsinExample.createCriteria();
        if(feedId!=null){
            criteria.andFeedidEqualTo(feedId);
        }
        if(depositTime!=null){
            criteria.andDeposittimeEqualTo(depositTime);
        }
        if(remark!=null){
            criteria.andRemarkLike("%"+remark+"%");
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<Feedsin> feedsins=feedsinMapper.selectByExample(feedsinExample);
        PageInfo info = new PageInfo(feedsins);
        return info;
    }

    /**
     *保存饲料入库信息
     * @param feedsin 饲料入库信息
     * @param oldNumInStore 原来的库存量
     * @return 1成功 0失败
     */
    public int save(Feedsin feedsin,Float oldNumInStore){
        int a=feedsinMapper.insert(feedsin);
        if(a==0){
            return a;
        }
        int feedid=feedsin.getFeedid();
        //若插入饲料入库信息表没出错，则修改饲料信息表的库存量
        Feeds feeds=feedsMapper.selectByPrimaryKey(feedsin.getFeedid());
        //库存量为原来的库存量+入库的数量
        feeds.setAmountStore(oldNumInStore+feedsin.getQuantity());
        int b=feedsMapper.updateByPrimaryKeySelective(feeds);
        if(b==0){
            return b;
        }
        return 1;
    }

    public int delete(Integer id){
        Feedsin feedsin= feedsinMapper.selectByPrimaryKey(id);
        //删除时需要设置外键为空才能删除成功
        feedsin.setFeedid(null);
        int a=feedsinMapper.updateByPrimaryKeySelective(feedsin);
        return feedsinMapper.deleteByPrimaryKey(id);
    }

    public  Feedsin findByInStoreId(Integer instoreid){
        FeedsinExample feedsinExample=new FeedsinExample();
        FeedsinExample.Criteria criteria=feedsinExample.createCriteria();
        if(instoreid!=null){
            criteria.andInstoreidEqualTo(instoreid);
            return feedsinMapper.selectByPrimaryKey(instoreid);
        }
        return null;
    }

    /**
     * 修改饲料入库信息
     * @param feedsin 饲料入库信息
     * @param oldInStoreNum 原来的入库数量
     * @return
     */
    public int update(Feedsin feedsin,Float oldInStoreNum){
        //更新饲料入库信息之前首先要更新饲料种类信息的库存量
        //获取前后库存量的差值(差值可能为负数,通过"新的入库数量-原来的入库数量"得出)
        float between=feedsin.getQuantity()-oldInStoreNum;
        Feeds feeds=feedsMapper.selectByPrimaryKey(feedsin.getFeedid());
        //库存量为现在的库存量+差值
        feeds.setAmountStore(feeds.getAmountStore()+between);
        int code=feedsMapper.updateByPrimaryKeySelective(feeds);
        //如果修改饲料种类信息的库存量失败就返回不再继续执行
        if(code==0){
            return 0;
        }

        //最后更新修改后的饲料入库信息
        feedsinMapper.updateByPrimaryKey(feedsin);
        return 1;
    }
}
